﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CivilRegistry.ControlledModel;
using CivilRegistry.Common;

namespace CivilRegistry
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SessionHandler.CurrentSession.User != null)
            {
                Response.Redirect(Request.QueryString["redirect"] ?? "~/Default.aspx");
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string url = Request.QueryString["redirect"] ?? "~/Default.aspx";
            var user = ControlledModel.User.GeUser(txtUserName.Text, txtPassword.Text);

            if (user != null)
            {
                if (user.DeactivateReason != null)
                {
                    if (user.DeactivateReason == "Not-yet-Activated")
                    {
                        lblStatus.Text = "The account is not activated.";
                    }
                    else
                    {
                        lblStatus.Text = "Your account is deactivated. Please check your mailbox for the reason or contact the administrator for more information.";
                    }
                }
                else
                {
                    SessionHandler.CurrentSession.User = user;
                    Response.Redirect(url);
                }
            }
            else
            {
                lblStatus.Text = "Invalid username or password.";
            }
        }
    }
}